package cn.tuling.org.service.impl;

import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.Department;
import cn.tuling.org.domain.User;
import cn.tuling.org.mapper.DepartmentMapper;
import cn.tuling.org.query.DepartmentQuery;
import cn.tuling.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findById(Long deptId) {
        return departmentMapper.findById(deptId);
    }

    @Override
    public void add(Department department) {
        departmentMapper.add(department);
    }

    @Override
    public void delete(Long deptId) {
        departmentMapper.delete(deptId);
    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Integer queryByCount(DepartmentQuery query) {
        return departmentMapper.queryByCount(query);
    }

    @Override
    public PageList<Department> queryByPage(DepartmentQuery query) {
        Integer totals = departmentMapper.queryByCount(query);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        List<Department> departments = departmentMapper.queryByPage(query);
        return new PageList<>(totals,departments);
    }
}
